package com.hmdp.controller;/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2024/10/25 17:01
 **/

import com.hmdp.dto.Result;
import com.hmdp.entity.Voucher;
import com.hmdp.service.IVoucherService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @description 预热
 * @author sats@jz
 * @date 2024年10月25日 17:01
 */
@RestController
@RequestMapping("/pre")
public class YureController {

    @Resource
    private IVoucherService voucherService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("addvoucherbyvoucher")
    public Result addvoucherbyvoucher(Voucher voucher) {
        voucherService.addSeckillVoucher(voucher);
        return Result.ok(voucher.getId());
    }

    @RequestMapping("addvoucherbynovoucher")
    @Transactional
    public Result addvoucherbynovoucher() {
        Voucher voucher = new Voucher();
        // 随机造一个
        voucher.setTitle("优惠券");
        voucher.setPayValue(10000L);
        voucher.setActualValue(50000L);
        voucher.setStock(100);
        voucher.setType(1);
        voucher.setStatus(1);
        voucher.setBeginTime(LocalDateTime.parse("2024-10-29T00:00:00"));
        voucher.setEndTime(LocalDateTime.parse("2024-10-29T23:59:59"));
        voucher.setShopId(1L);
        voucher.setSubTitle("优惠券");
        voucher.setRules("满100减50");
        voucher.setCreateTime(LocalDateTime.parse("2024-10-25T17:30:00"));
        voucher.setUpdateTime(null);

//        // 存入redis,开始时间，结束时间，库存，时间转换为时间戳
//        stringRedisTemplate.opsForHash().put("seckill:stock:"+voucher.getId(),"start_time", ""+voucher.getBeginTime().toEpochSecond(ZoneOffset.UTC));
//        stringRedisTemplate.opsForHash().put("seckill:stock:"+voucher.getId(),"end_time",""+voucher.getEndTime().toEpochSecond(ZoneOffset.UTC));
//        stringRedisTemplate.opsForHash().put("seckill:stock:"+voucher.getId(),"stock",voucher.getStock().toString());
//        // 创建一个orderKey = 'seckill:order:' .. voucherId set
////        stringRedisTemplate.opsForSet().add("seckill:order:"+voucher.getId(),"");

        voucherService.addSeckillVoucher(voucher);
        return Result.ok(voucher.getId());
    }


}
